<?php

namespace App\Models;

class Category extends Base
{
    protected $table = 'categorys';

    const GROUP_1 = 1;

    const GROUP_TEXT = [
        self::GROUP_1 => '默认',
    ];

    protected $fillable = [
        'id','title','pid','thumb','group','sort','created_at','updated_at'
    ];




    protected function list($params = [], $fields = ['*']){
        return $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                $query ->where('id','>',1);
            })->orderBy('id', 'desc')
            ->paginate(config('setting.limit'));
    }

    protected function tree(){
        $tree = $this->select(['id as value','title as label', 'pid'])->get()->toArray();
        return $this->listToTree($tree ,'value');
    }

}
